package com.hwua.tonyshop.controller.bg;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hwua.tonyshop.criteria.MenuInfoCriteria;
import com.hwua.tonyshop.entity.mojo.MenuInfoAO;
import com.hwua.tonyshop.entity.pojo.MenuInfo;
import com.hwua.tonyshop.service.IMenuInfoService;
import com.slkj.autocode.entity.ServiceResult;
import com.slkj.autocode.util.DateTimeUtils;

/**
 * 
 * @author sunny Yang
 * @date 2018年5月24日
 * @discribe
 * 菜单管理
 *
 */
@Controller
@RequestMapping(value = "/back/menu")
public class MenuController {
	@Resource
	IMenuInfoService menuInfoService;
	/**
	 * 跳转到菜单管理界面
	 * @param backToken
	 * @param model
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/toMenuManagerPage", method = { RequestMethod.GET, RequestMethod.POST })
	public String toMenuManagerPage(String backToken,Model model,HttpServletRequest request,HttpServletResponse response) {
	  	
		model.addAttribute("backToken", backToken);
		return "background/oz_menu";
	}
	
	@RequestMapping(value = "/getMenu", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public ServiceResult<MenuInfoAO> getMenu(Integer menuId,String backToken,Model model,HttpServletRequest request,HttpServletResponse response) {
		return menuInfoService.selectById(menuId);
	}
	
	@RequestMapping(value = "/getMenus", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public ServiceResult<List<MenuInfoAO>> getMenus(Model model,HttpServletRequest request,HttpServletResponse response) {
		MenuInfoCriteria criteria = new MenuInfoCriteria();
		return menuInfoService.selectByCriteria(criteria);
	}
	
	@RequestMapping(value = "/deleteMenu", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public ServiceResult<Integer> deleteMenu(Integer menuId,String backToken,Model model,HttpServletRequest request,HttpServletResponse response) {
		return menuInfoService.deleteById(menuId);
	}
	
	@RequestMapping(value = "/addMenu", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public ServiceResult<Integer> addMenu(MenuInfo menuInfo,String backToken,Model model,HttpServletRequest request,HttpServletResponse response) {
		menuInfo.setState("0");
		menuInfo.setCreateDate(DateTimeUtils.formateDateToStr(new Date()));
		return menuInfoService.addMenuInfo(menuInfo);
	}
	
	@RequestMapping(value = "/updateMenu", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public ServiceResult<Integer> updateMenu(MenuInfo menuInfo,String backToken,Model model,HttpServletRequest request,HttpServletResponse response) {
		return menuInfoService.updateById(menuInfo);
	}
}
